package com.tianpengtech.front.mobile.common.intercepter;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.util.IpKit;
import com.tianpengtech.front.mobile.common.constant.ConstantApi;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.ToolMD5;
import com.tianpengtech.front.web.login.LoginService;

import java.text.Collator;
import java.util.*;

/**
 *  MobileRequest intercept
 * @author ggd
 */
public class MobileRequestInterceptor implements Interceptor {

    public void intercept(Invocation inv) {
        Controller c=inv.getController();
        Comparator<Object> com= Collator.getInstance(Locale.US);
        List<String> list1 = new ArrayList<>(c.getParaMap().keySet());
        Collections.sort(list1,com);
        StringBuffer stringBuffer = new StringBuffer();
        for (String str:list1) {
            if (!str.equals("security")){
                stringBuffer.append(str+"="+c.getPara(str)+"&");
            }
        }
        stringBuffer.append(PropKit.get("config.client.salt"));
        String encode= ToolMD5.encodeMD5Hex(stringBuffer.toString());
        String sct=c.getPara("security");
        if(StrKit.notBlank(sct)){
            if (sct.equals(encode)){
                inv.invoke();
            }else{
                c.renderJson(R.fail("数据无效"));
            }
        }else{
            c.renderJson(R.fail("数据无效"));
        }
    }
}
